package com.lovo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.system.dto.PageDto;
import com.lovo.system.dto.RoleDto;
import com.lovo.system.dto.RolePermissionsDto;
import com.lovo.system.service.IRolePermissionsService;
import com.lovo.system.service.IRoleService;
import com.lovo.system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @Autowired
    IRolePermissionsService rolePermissionsService;

    @RequestMapping("/info")
    public IPage<RoleDto> info(@RequestBody PageDto<RoleDto> pageDto){
        return roleService.cutByItem(pageDto.getCurPage(),pageDto.getPageSize());
    }

    @RequestMapping("/selectList")
    public List<RoleDto> selectList(){
        return roleService.selectList();
    }

    @RequestMapping("/selectOne")
    public RoleDto selectOne(int id){
        return roleService.selectOne(id);
    }

    @RequestMapping("/selectUserRoleNow")
    public List<Integer> selectUserRoleNow(int id){
        return roleService.selectUserRoleNow(id);
    }

    @RequestMapping("/selectRolePer")
    public List<String> selectRolePer(@RequestBody List<Integer> ids){
        return roleService.selectRolePer(ids);
    }

    @RequestMapping("/selectRolePerIds")
    public List<Integer> selectRolePerIds(@RequestBody List<Integer> ids){
        return roleService.selectRolePerIds(ids);
    }

    @RequestMapping("/selectRolePerNow")
    public List<Integer> selectRolePerNow(int id){
        return roleService.selectRolePerNow(id);
    }

    @RequestMapping("/update")
    @Transactional
    public ResponseResult updateRole(int[] permissionsIds,int roleId){
        ResponseResult responseResult = new ResponseResult();
        rolePermissionsService.deleteRolePermissionsByRole(roleId);
        for (int permissionsId : permissionsIds){
            RolePermissionsDto r = new RolePermissionsDto();
            r.setRoleId(roleId);
            r.setPermissionsId(permissionsId);
            rolePermissionsService.insertRolePermissions(r);
        }
        return responseResult;
    }

    @RequestMapping("/add")
    @Transactional
    public ResponseResult insertRole(String roleName,int[] permissionsIds){
        ResponseResult responseResult = new ResponseResult();
        RoleDto roleDto = new RoleDto();
        roleDto.setRoleName(roleName);
        int roleId = roleService.insertRole(roleDto);
        for (int permissionsId : permissionsIds){
            RolePermissionsDto r = new RolePermissionsDto();
            r.setRoleId(roleId);
            r.setPermissionsId(permissionsId);
            rolePermissionsService.insertRolePermissions(r);
        }
        return responseResult;
    }

    @RequestMapping("/delete")
    @Transactional
    public ResponseResult deleteRole(int roleId){
        ResponseResult responseResult = new ResponseResult();
        roleService.deleteRole(roleId);
        rolePermissionsService.deleteRolePermissionsByRole(roleId);
        return responseResult;
    }

}
